package com.example.casper.jnuexperiment2025;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

public class BookDetailsActivity extends AppCompatActivity {

    private EditText editTextBookTitle;
    private int bookPosition = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_details);

        editTextBookTitle = findViewById(R.id.edittext_book_title);
        Button buttonOk = findViewById(R.id.button_book_details_ok);

        // 获取传递过来的数据
        Intent intent = getIntent();
        if (intent != null) {
            String title = intent.getStringExtra("title");
            bookPosition = intent.getIntExtra("book_position", -1);

            if (title != null) {
                editTextBookTitle.setText(title);
            } else {
                editTextBookTitle.setHint("请输入图书标题");
            }
        }

        buttonOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent resultIntent = new Intent();
                String title = editTextBookTitle.getText().toString().trim();

                if (!title.isEmpty()) {
                    resultIntent.putExtra("title", title);
                    resultIntent.putExtra("book_position", bookPosition);
                    setResult(Activity.RESULT_OK, resultIntent);
                } else {
                    setResult(Activity.RESULT_CANCELED);
                }
                finish();
            }
        });
    }
}